<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\UserComment;
use yii\db\Query;

/**
 * UserCommentSearch represents the model behind the search form about `backend\models\UserComment`.
 */
class UserCommentSearch extends UserComment
{
    public $create_time,$view_img;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['send_time','content_type'],'string'],
            [['id', 'content_id', 'send_uid', 'comment_id', 'like'], 'integer'],
            [['content_type', 'comment'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query1 = UserComment::find()->select(['*','Noid'=>'`comment_id` + `id`','9999999999 `t_order`'])->where(['comment_id'=>0]);
        $query2 = UserComment::find()->select(['*','Noid'=>'`comment_id`', '`t_order`'=>'send_time'])->where(['>','comment_id',0]);
        $query = UserComment::find()->select(['*'])->from(['A'=>$query1->union($query2)]);
        //->orderBy(['Noid'=>SORT_DESC,'t_order'=>SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=>['defaultOrder'=>['Noid'=>SORT_DESC,'t_order'=>SORT_DESC]]
        ]);
        $dataProvider->sort->attributes['Noid'] = [
            'asc'  => ['Noid' => SORT_ASC],
            'desc' => ['Noid' => SORT_DESC],
        ];
        $dataProvider->sort->attributes['t_order'] = [
            'asc'  => ['t_order' => SORT_ASC],
            'desc' => ['T_order' => SORT_DESC],
        ];
        $this->load($params);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'content_id' => $this->content_id,
            'send_uid' => $this->send_uid,
            'like' => $this->like,
        ]);
        if (empty($this->send_time)){
            $startTime = null;
            $endTime = null;
        }else {
            $startTime = strtotime($this->send_time);
            $endTime = $startTime + 24*60*60;
        }
        $query->andFilterWhere(['like', 'content_type', $this->content_type])
            ->andFilterWhere(['between', 'send_time',$startTime, $endTime])
            ->andFilterWhere(['like', 'comment', $this->comment])
            ->andFilterWhere(['=','id',$this->id])
            ->andFilterWhere(['=', 'comment_id', $this->comment_id]);
            //->andFilterWhere('or',['=','id',$this->content_id],['=', 'comment_id', empty($this->comment_id)? 0 : $this->comment]);

        return $dataProvider;
    }
}
